﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SnongPhoneShop.Models;

namespace SnongPhoneShop.Controllers
{
    public class Tim_KiemController : Controller
    {
        //
        // GET: /Tim_Kiem/
        StoredProcedure sp = new StoredProcedure();
        int pageSize = 4;

        public ActionResult Index(int? page, string Keyword, string Start_Price, string End_Price, int? CatalogTypeID, int? CatalogID)
        {
            if (page == null) page = 1;
            int startPrice, endPrice;
            int.TryParse(Start_Price, out startPrice);
            int.TryParse(End_Price, out endPrice);
            ViewBag.Start_Price = startPrice == 0 ? "" : Start_Price;
            ViewBag.End_Price = endPrice == 0 ? "" : End_Price;
            if (CatalogTypeID == null)
            {
                ViewBag.CatalogTypeID = 0;
                ViewBag.CatalogTypeList = new SelectList(sp.GetListTypeCatalog(), "MaLoaiDanhMuc", "TenLoaiDanhMuc");
            }
            else
            {
                ViewBag.CatalogTypeID = CatalogTypeID;
                ViewBag.CatalogTypeList = new SelectList(sp.GetListTypeCatalog(), "MaLoaiDanhMuc", "TenLoaiDanhMuc", CatalogTypeID);
            }
            if (CatalogID == null)
            {
                ViewBag.CatalogID = 0;
                ViewBag.CatalogLists = new SelectList(sp.GetListCatalogByTypeID(0), "MaDanhMuc", "TenDanhMuc");
            }
            else
            {
                ViewBag.CatalogID = CatalogID;
                ViewBag.CatalogLists = new SelectList(sp.GetListCatalogByTypeID((int)CatalogTypeID), "MaDanhMuc", "TenDanhMuc", CatalogID);
            }

            ViewBag.OrderBy = new SelectList(new List<OrderByList>{
                    new OrderByList{Name = "Tên sản phẩm", Value = 4},
                    new OrderByList{ Name = "Giá", Value = 1},
                    new OrderByList{Name = "Tên danh mục", Value = 2},
                    new OrderByList{Name = "Ngày phát hành", Value = 3}
                }, "Value", "Name");

            ViewBag.OrderType = new SelectList(new List<OrderTypeList>{
                    new OrderTypeList{ Name = "Ascending", Value = true},
                    new OrderTypeList{Name = "Descending", Value = false},
                }, "Value", "Name");
            if (Keyword == null)
            {
                ViewBag.Keyword = "";
                return View();
            }
            else
            {
                ViewBag.Keyword = Keyword;
                int pageCount, startPage, endPage;
                var T = sp.GetListProductByName(Keyword, startPrice, endPrice, (int)CatalogTypeID, (int)CatalogID);
                var L = sp.PagingListProduct(T, (int)page, pageSize, out pageCount, out startPage, out endPage);
                ViewBag.pageCount = pageCount;
                ViewBag.pageIndex = page;
                ViewBag.startPage = startPage;
                ViewBag.endPage = endPage;
                ViewBag.totalProduct = T.Count;

                ViewBag.SortModel = new SortModel
                {
                    SanPhams = T,
                    Type = 4,
                    Ascending = true
                };

                return View(L);
            }
        }

        [HttpPost]
        public ActionResult Index(FormCollection Fc)
        {
            string Keyword = Fc["Search-Input-In-Layout"];
            return RedirectToAction("Index", new { 
                Keyword = Keyword,
                CatalogTypeID = 0,
                CatalogID = 0
            });
        }

        [HttpPost]
        public ActionResult Search(FormCollection Fc)
        {
            string Keyword = Fc["Search-Input-In-Page"];
            int Start_Price, End_Price;
            int.TryParse(Fc["Start-Price-Value"], out Start_Price);
            int.TryParse(Fc["End-Price-Value"], out End_Price);
            int CatalogTypeID, CatalogID;
            int.TryParse(Fc["CatalogType"], out CatalogTypeID);
            int.TryParse(Fc["CatalogList"], out CatalogID);
            return RedirectToAction("Index", new { 
                Keyword = Keyword,
                Start_Price = Start_Price,
                End_Price = End_Price,
                CatalogTypeID = CatalogTypeID,
                CatalogID = CatalogID 
            });
        }

        public ActionResult GotoPage(int page)
        {
            int pageCount, startPage, endPage;
            var T = (SortModel)TempData["SortModel"];
            var L = sp.PagingListProduct(T.SanPhams, page, pageSize, out pageCount, out startPage, out endPage);
            ViewBag.pageCount = pageCount;
            ViewBag.pageIndex = page;
            ViewBag.startPage = startPage;
            ViewBag.endPage = endPage;
            ViewBag.SortModel = TempData["SortModel"];
            return PartialView("SearchResult", L);
        }

        public ActionResult Sort(int OrderBy, string OrderType)
        {
            var T = (SortModel)TempData["SortModel"];
            T.Type = OrderBy;
            T.Ascending = bool.Parse(OrderType);
            var sortModel = new SortModel
            {
                Type = OrderBy,
                Ascending = bool.Parse(OrderType),
                SanPhams = T.GetSortedProduct()
            };
            
            ViewBag.SortModel = sortModel;
            int pageCount, startPage, endPage;
            var L = sp.PagingListProduct(sortModel.SanPhams, 1, pageSize, out pageCount, out startPage, out endPage);
            ViewBag.pageCount = pageCount;
            ViewBag.pageIndex = 1;
            ViewBag.startPage = startPage;
            ViewBag.endPage = endPage;
            return PartialView("SearchResult", L);
        }
    }

    public class OrderByList
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }

    public class OrderTypeList
    {
        public string Name { get; set; }
        public bool Value { get; set; }
    }
}
